<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ProductRepo;
use App\Repos\UserGroupbuyRepo;
use Carbon\Carbon;

class UsergroupController extends Controller
{
    

    public function show(Request $request)
    {
        $rs['status']     = false;        
       
        $map['code']    = $request->group_code;
        $map['user_id'] = $request->user_id;
        $ug = UserGroupbuyRepo::find($map);
        if($ug->status=='100' || $ug->status=='-1') {
            $rs['msg'] = '拼购活动已结束';
            return response()->json($rs); 
        } 

       

        $group = $userGroup = [];
        $where['id'] = $ug->product_id;
        $fields    = ['id','type_id','name','thumb','price','origin_price','spec_id','weight'];
        $data      = ProductRepo::find($where,$fields);  
        // 团购商品才有详情
        
            $group     = ProductRepo::group($ug->product_id);
            if( object_get($group,'id') ) {
                $userGroup = ProductRepo::userGroupbuy($group->id);       
            }
             
             
       
        if($data) {
            $rs['status'] = true;
            $rs['data']  = $data;
            $rs['data']['group'] = $group;
            $rs['data']['user_group'] = $userGroup;  
            $rs['data']['spec_name'] = object_get($data,'spec.name');       
           
        }
        return response()->json($rs);       
    }

    public function group(request $request)
    {
        $rs['status']     = false;        
        $group     = ProductRepo::group($request->product_id);
        $data = ProductRepo::userGroupbuy($group->id);
        if($data) {
            $rs['status'] = true;           
            $rs['data']['list']   = $data->sortByDesc('num');    
            $rs['data']['group_num'] = $group->group_num;  // 团购所需人数
            $rs['data']['total_num'] = $group->total_num;  //组团人数

            
           
        }
        return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);   
    }
}
